# ============================================================== # It is recommended to test the script on a local machine for its purpose and effects. # ManageEngine Endpoint Central will not be responsible for any # damage/loss to the data/setup based on the behavior of the script. # Description: Script to clear global windows cache (Temp, Prefetch, $Recycle.bin) # Parameters: # Remarks: # Configuration Type - COMPUTER # ============================================================== Function Clear-GlobalWindowsCache { Remove-CacheFiles 'C:\Windows\Temp' Remove-CacheFiles "C:\`$Recycle.Bin" Remove-CacheFiles "C:\Windows\Prefetch" #C:\Windows\System32\rundll32.exe InetCpl.cpl, ClearMyTracksByProcess 255 #C:\Windows\System32\rundll32.exe InetCpl.cpl, ClearMyTracksByProcess 4351 } Function Remove-CacheFiles { param([Parameter(Mandatory=$true)][string]$path) BEGIN { $originalVerbosePreference = $VerbosePreference $VerbosePreference = 'Continue' } PROCESS { if((Test-Path $path)) { if([System.IO.Directory]::Exists($path)) { try { if($path[-1] -eq '\') { [int]$pathSubString = $path.ToCharArray().Count - 1 $sanitizedPath = $path.SubString(0, $pathSubString) Remove-Item -Path "$sanitizedPath\*" -Recurse -Force -ErrorAction SilentlyContinue -Verbose } else { Remove-Item -Path "$path\*" -Recurse -Force -ErrorAction SilentlyContinue -Verbose } } catch { } } else { try { Remove-Item -Path $path -Force -ErrorAction SilentlyContinue -Verbose } catch { } } } } END { $VerbosePreference = $originalVerbosePreference } } Clear-GlobalWindowsCache